from django.db import models
from django.db.models import Q

class user(models.Model):
    seq = models.AutoField(primary_key=True)
    uid = models.CharField(max_length=20, db_index=True, unique=True)
    uName = models.CharField(max_length=255, db_index=True)
    qzoneName = models.CharField(max_length=255, db_index=True)
    qzoneDescription = models.CharField(max_length=255)
    articleNum = models.IntegerField(db_index=True)
    done = models.BooleanField(default=False, db_index=True)
    modtime = models.DateTimeField()
    numClasses = models.IntegerField(default=0)

    @classmethod
    def getArrBySeq(cls, seqs):
        condition = None
        for s in seqs:
            if not condition:
                condition = Q(seq = s)
            else:
                condition = (condition | Q(seq = s))
        if condition == None:
            return []
        res = cls.objects.using('default').filter(condition)
        ret = []
        for s in seqs:
            bingo = False
            for u in res:
                if u.seq == s:
                    bingo=True
                    ret.append(u)
                    #res.remove(u) 
                    break
            if not bingo:
                ret.append(None)
        return ret

    def __unicode__(self):
        return ("A record in table 'user' seq(%d)" % self.seq) + " uid(" + self.uid + ")" 

    class Admin: 
        pass 

class blog(models.Model):
    seq = models.AutoField(primary_key=True)
    blog_id = models.CharField(max_length=32, db_index=True)
    authorid = models.CharField(max_length=20, db_index=True)
    title = models.CharField(max_length=1024)
    time = models.DateTimeField(db_index=True)
    num_comment = models.IntegerField(db_index=True)
    num_read = models.IntegerField(db_index=True)
    content = models.TextField()
    plaintext = models.TextField()
    cat_id = models.IntegerField(default=-1, db_index=True)
    cat_name = models.TextField(max_length=32, default='')
    #CREATE UNIQUE INDEX idx_blog_primary2 ON crawlqzone.blog(blog_id, authorid);

    def __unicode__(self):
        return ("A Record in table 'blog' seq(%d)" % self.seq) + " blog_id(" + self.blog_id + ") title(" + self.title + ")"

    class Admin: 
        pass

    @classmethod
    def getArrBySeq(cls, seqs):
        condition = None
        for s in seqs:
            if not condition:
                condition = Q(seq = s)
            else:
                condition = (condition | Q(seq = s))
        if condition == None:
            return []
        res = cls.objects.using('default').filter(condition)
        ret = []
        for s in seqs:
            bingo = False
            for u in res:
                if u.seq == s:
                    bingo=True
                    ret.append(u)
                    #res.remove(u) 
                    break
            if not bingo:
                ret.append(None)
        return ret

class userpie(models.Model):
    uid = models.CharField(max_length=20, db_index=True, primary_key=True)
    cat_id = models.IntegerField(default=-1, db_index=True)
    cat_name = models.CharField(max_length=32, default='')
    blog_ids = models.TextField(default='')
    keywords = models.CharField(max_length=256)
    #CREATE UNIQUE INDEX idx_userpie_primary ON crawlqzone.userpie(id, cat_id);
    def __unicode__(self):
        return "A Record in table 'userpie' uid(%s) cat_id(%d) blogs(%s)" % (self.uid, self.cat_id, self.blog_ids)
    class Admin: 
        pass 

    @classmethod
    def getArrByUid(cls, uids):
        condition = None
        for u in uids:
            if not condition:
                condition = Q(uid = u)
            else:
                condition = (condition | Q(uid = u))
        if condition == None:
            return []
        res = cls.objects.using('default').filter(condition)
        ret = {}
        for u in uids:
            bingo = False
            for up in res:
                if up.uid == u:
                    bingo = True
                    if ret.has_key(u):
                        ret[u].append(up)
                    else:
                        ret[u]=[up]
                    #res.remove(up)
            if not bingo:
                ret[u] = []
        return ret

class usernoblog(models.Model):
    uid = models.CharField(max_length=16, db_index=True, primary_key=True)
    modtime = models.DateTimeField(db_index=True)
    def __unicode__(self):
        return "A Record in table 'usernoblog' uid(" + self.uid + ") modtime(" + self.modtime + ")"

    class Admin: 
        pass 

class profile(models.Model): 
    uin = models.CharField(max_length=20, primary_key=True)
    nickname = models.CharField(max_length=255)
    sex = models.CharField(max_length=4)
    age = models.IntegerField()
    birthday = models.DateField()
    bloodtype = models.CharField(max_length=2)
    country = models.CharField(max_length=64)
    province = models.CharField(max_length=64)
    city = models.CharField(max_length=64)
    hco = models.CharField(max_length=64)
    hp = models.CharField(max_length=64)
    hc = models.CharField(max_length=64)
    marriage = models.IntegerField()
    spacename = models.CharField(max_length=255)
    descri = models.CharField(max_length=255)
    interest = models.TextField()
    career = models.CharField(max_length=64)
    company = models.CharField(max_length=64)
    cco = models.CharField(max_length=64)
    cp = models.CharField(max_length=64)
    cb = models.CharField(max_length=64)
    cr = models.IntegerField()
    needpage = models.IntegerField()
    hcr = models.IntegerField()
    mb = models.IntegerField()
    cm = models.IntegerField()
    done = models.BooleanField(default=False, db_index=True)
    modtime = models.DateTimeField()
    def __unicode__(self):
        return self.uin + "_" + self.nickname
        return "A Record in table 'usernoblog' uid(" + self.uin + ") nickname(" + self.nickname + ")"
    class Admin: 
        pass 

    @classmethod
    def getArrByUid(cls, uids):
        condition = None
        for u in uids:
            if not condition:
                condition = Q(uin = u)
            else:
                condition = (condition | Q(uin = u))
        if condition == None:
            return []
        res = cls.objects.using('default').filter(condition)
        ret = []
        for u in uids:
            bingo = False
            for p in res:
                if p.uin == u:
                    bingo=True
                    ret.append(p)
                    #res.remove(u) 
                    break
            if not bingo:
                ret.append(None)
        return ret
